package com.devbobcorn.nekoration.blocks;

import com.devbobcorn.nekoration.NekoConfig;
import com.devbobcorn.nekoration.NekoConfig.VerConnectionDir;
import com.devbobcorn.nekoration.blocks.entities.ItemDisplayBlockEntity;
import com.devbobcorn.nekoration.common.VanillaCompat;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;

public class CupboardBlock extends ItemDisplayBlock {
    public static final BooleanProperty BOTTOM  = BlockStateProperties.BOTTOM;
	public final boolean playSound;

    public CupboardBlock(Properties settings, boolean p) {
        super(settings);
		this.playSound = p;
    }

    protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> s) {
		s.add(COLOR, FACING, OPEN, BOTTOM);
	}
	
	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {
		return new ItemDisplayBlockEntity(false, playSound);
	}

	public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
		ItemStack itemStack = player.getItemInHand(hand);

		if (world.isClientSide) {
			return (VanillaCompat.COLOR_ITEMS.containsKey(itemStack.getItem())) ? ActionResultType.SUCCESS : super.use(state, world, pos, player, hand, hit);
		}
		
		if (VanillaCompat.COLOR_ITEMS.containsKey(itemStack.getItem())) {
			world.setBlock(pos, state.setValue(COLOR, VanillaCompat.COLOR_ITEMS.get(itemStack.getItem())), 3);
			return ActionResultType.CONSUME;
		}
		return super.use(state, world, pos, player, hand, hit);
	}

	public BlockState getStateForPlacement(BlockItemUseContext ctx) {
		World blockView = ctx.getLevel();
		BlockPos blockPos = ctx.getClickedPos();
		VerConnectionDir config = NekoConfig.SERVER.verConnectionDir.get();
		
		if (config == VerConnectionDir.BOTH || config == VerConnectionDir.TOP2BOTTOM){
			BlockPos blockPosRef = blockPos.above();
			BlockState stateRef = blockView.getBlockState(blockPosRef);

			if (stateRef.getBlock() instanceof CupboardBlock){
				return super.getStateForPlacement(ctx).setValue(BOTTOM, true);
			} else return super.getStateForPlacement(ctx).setValue(BOTTOM, false);
		}
		return super.getStateForPlacement(ctx).setValue(BOTTOM, false);
	}

	public BlockState updateShape(BlockState state, Direction direction, BlockState newState, IWorld world, BlockPos pos, BlockPos posFrom) {
		VerConnectionDir config = NekoConfig.SERVER.verConnectionDir.get();

		if (config == VerConnectionDir.BOTH || config == VerConnectionDir.BOTTOM2TOP)
			if (direction == Direction.UP)
				if (newState.getBlock() instanceof CupboardBlock)
					return state.setValue(BOTTOM, true);
				else return state.setValue(BOTTOM, false);
		return state;
	}
}
